package com.cykj.auth.service;

import com.cykj.auth.form.LoginBody;
import com.cykj.common.core.constant.Constants;
import com.cykj.common.core.domain.R;
import com.cykj.common.core.exception.ServiceException;
import com.cykj.system.api.RemoteShopUserService;
import com.cykj.system.api.domain.BsUser;
import com.cykj.system.api.model.BsLoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Program: Cykj-Order-System
 * @Package: com.cykj.auth.service
 * @Author: 张俊杰
 * @Create: 2025-08-22 00:41
 * @Description: 商家登录服务
 * @Vision: 1.0
 **/
@Component
public class ShopLoginService {

    @Resource
    private RemoteShopUserService remoteShopUserService;

    @Autowired
    private SysRecordLogService recordLogService;


    public BsLoginUser login(String phone, String password){

        if (StringUtils.isAllBlank(phone,password)){
            recordLogService.recordLogininfor(phone, Constants.LOGIN_FAIL, "手机号/密码必须填写");
            throw new ServiceException("手机号/密码必须填写");
        }

        R<BsLoginUser> shopUserInfoByName = remoteShopUserService.getShopUserInfoByName(phone);

        BsLoginUser bsLoginUser = shopUserInfoByName.getData();

        BsUser bsUser = bsLoginUser.getBsUser();

        if (!password.equals(bsUser.getPassword())){
            throw new ServiceException("用户不存在/密码错误");
        }

        return bsLoginUser;
    }

}
